<script setup>
import { ref, reactive, onUnmounted } from "vue";
import { useRouter } from "vue-router";
import ImgCaptcha from "@/a-components/less/ImgCaptcha.vue";

let router = useRouter();

const formRef = ref(null); // 获取表单实例
const formData = ref({
  userEmail: "",
  userPassword: "",
});
const showNonRobotVerify = ref(false); // 是否显示验证码

const rules = reactive({
  userEmail: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    {
      type: "email",
      message: "请输入正确的邮箱格式",
      trigger: ["blur", "change"],
    },
  ],
  userPassword: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

async function login() {
  formRef.value.validate(async (valid) => {
    if (!valid) return;
    showNonRobotVerify.value = true;
  });
}

function register() {
  router.push("/Signin");
}

const closeNonRobotVerify = () => {
  showNonRobotVerify.value = false;
};
</script>

<template>
  <div class="container">
    <h1>邮箱登录</h1>

    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="邮箱账号:" prop="userEmail">
        <el-input
          v-model="formData.userEmail"
          placeholder="请输入邮箱"
        ></el-input>
      </el-form-item>

      <el-form-item label="密码:" prop="userPassword">
        <el-input
          v-model="formData.userPassword"
          type="password"
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
    </el-form>

    <!-- 登录 & 注册 按钮 -->
    <div class="btn-group">
      <el-button type="success" @click="login">登录</el-button>
      <el-button type="info" @click="register">注册</el-button>
    </div>
  </div>

  <ImgCaptcha :showNonRobotVerify="showNonRobotVerify" :loginInfo="formData" @close="closeNonRobotVerify" class="captcha-container"></ImgCaptcha>
</template>

<style scoped>
.container {
  max-width: 500px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 30px;
  background: white;
  border: 1px solid #512a10;
  border-radius: 12px;
  overflow-y: auto;
}

h1 {
  text-align: center;
  font-size: 18px;
  margin-bottom: 10px;
}

.btn-group {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 15px;
}

.btn-group .el-button {
  width: 100px;
}

.captcha-container {
  height: 30%;
}
</style>
